<?php
/**
 * Created by PhpStorm.
 * User: yaoihu
 * Date: 2019/5/9
 * Time: 0:06
 */

namespace App\Http\Controllers\Shop;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Shop\ShopAdInfoModel;
use Illuminate\Http\Request;
use App\Models\Shop\AdPosModel;
use App\Models\Shop\AdModel;
use App\Helpers\Search;

class ShopAdController extends BackendController
{

    /**
     * @title 广告轮播图
     * @param Request $request
     * @return View
     */
    public function slideAd(Request $request)
    {
        $list = ShopAdInfoModel::paginate($this->getPerPageNum());

        return view('backend::shop.ad.slide_list',[
            'page_title'=>'轮播图列表',
            'list'=>$list
        ]);
    }

    /**
     * @title 添加轮播图
     * @param Request $request
     * @return SelfResponse|View
     */
    public function createSlide(Request $request)
    {
        if($request->isMethod('post')){
            if(!$request->post('title')){
                return SelfResponse::error('请输入标题');
            }
            if(!$request->post('img_url')){
                return SelfResponse::error('请上传图片');
            }
            $adInfo = new ShopAdInfoModel();
            $adInfo->title = $request->post('title');
            $adInfo->img_url = $request->post('img_url');
            $adInfo->url = $request->post('url');
            $adInfo->type = $request->post('type');
            $adInfo->status = $request->post('status');
            if($adInfo->save()){
                return SelfResponse::success('轮播添加成功','b_ShopAd_slideAd');
            }
            return SelfResponse::error('添加失败');
        }else{
            return view('backend::shop.ad.slide_add_or_edit',[
                'page_title'=>'添加轮播图',
                'url'=>url()->current()
            ]);
        }
    }

    /**
     * @title 编辑轮播图
     * @param Request $request
     * @param $ad_id
     * @return SelfResponse|View
     */
    public function updateSlide(Request $request,$ad_id)
    {
        $info = ShopAdInfoModel::find($ad_id);
        if($request->isMethod('post')){
            $info->title = $request->post('title');
            $info->img_url = $request->post('img_url');
            $info->url = $request->post('url');
            $info->type = $request->post('type');
            $info->status = $request->post('status');
            if($info->save()){
                return SelfResponse::success('编辑成功','b_ShopAd_slideAd');
            }
            return SelfResponse::error('编辑失败');
        }else{
            return view('backend::shop.ad.slide_add_or_edit',[
                'page_title'=>'添加轮播图',
                'info'=>$info,
                'url'=>url()->current()
            ]);
        }
    }

    /**
     * @title 删除轮播图
     * @param Request $request
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function deleteSlide(Request $request)
    {
        $ids = $request->input('slide_ids');
        $ids = array_filter(is_array($ids)?$ids:str2arr($ids));
        if(!$ids){
            return SelfResponse::error('请选择要操作的数据');
        }
        if(!ShopAdInfoModel::whereIn('id',$ids)->delete()){
            return SelfResponse::error('删除失败');
        }
        return SelfResponse::success('删除成功','b_ShopAd_slideAd');
    }

    public function adPositions(Request $request)
    {

    }


    /**
     * @title 广告管理
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        $search = Search::make($request)->bindModel(AdModel::with('pos'))
            ->time('created_at','创建时间')
            ->bindSearchData();
        $model = $search->where();

        $pos_id = $request->input('pos_id',0);
        if($pos_id){
            $model = $model->where('pos_id',$pos_id);
        }

        $list = $model->orderBy('pos_id','asc')->paginate($this->getPerPageNum());
        return view('backend::shop.ad.index',[
            'page_title'=>'广告管理',
            'list'=>$list,
            'search_tpl'=>$search,
            'pos_id'=>$pos_id
        ]);
    }

    /**
     * @title 广告位
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function adPos(Request $request)
    {
        $search = Search::make($request)->bindModel(AdPosModel::with('ad'))
            ->time('created_at','创建时间')
            ->bindSearchData();

        $list = $search->where()->where('status',1)
            ->orderByRaw('case when ad_number <=use_number then 100 else 0 end asc')
            ->paginate($this->getPerPageNum());

        return view('backend::shop.ad.ad_pos',[
            'page_title'=>'广告位',
            'list'=>$list,
            'search_tpl'=>$search
        ]);
    }

    public function create(Request $request,$pos_id)
    {
        if($request->isMethod('post')){
            $data = $request->post();
            $info = new AdModel();
            $info->ad_name = $data['ad_name'];
            $info->resource_link = $data['resource_link'];
            $info->status = $data['status'];
            $info->ad_link = $data['ad_link'];
            $info->pos_id = $pos_id;
            if($info->save()){
                AdPosModel::where('id',$pos_id)->increment('use_number',1);
                return SelfResponse::success('广告添加成功',['b_ShopAd_index',['pos_id'=>$pos_id]]);
            }
            return SelfResponse::error('广告添加失败');
        }else{
            $ad_pos = AdPosModel::where('status',1)->whereRaw('`ad_number`>`use_number`')->where('id',$pos_id)->first();
            if(!$ad_pos){
                return SelfResponse::error('广告位已满或已停用');
            }
            return view('backend::shop.ad.create_and_edit',[
                'page_title'=>'添加广告',
                'pos_info'=>$ad_pos
            ]);
        }
    }

    public function update(Request $request,$ad_id)
    {
        if($request->isMethod('post')){
            $info = AdModel::find($ad_id);
            $data = $request->post();
            $info->ad_name = $data['ad_name'];
            $info->resource_link = $data['resource_link'];
            $info->status = $data['status'];
            $info->ad_link = $data['ad_link'];
            if($info->save()){
                if($info->status<1){
                    AdPosModel::where('id',$info->pos_id)->decrement('use_number',1);
                }
                return SelfResponse::success('广告编辑成功',['b_ShopAd_index',['pos_id'=>$info->pos_id]]);
            }
            return SelfResponse::error('广告编辑失败');
        }else{
            $info = AdModel::find($ad_id);
            $ad_pos = AdPosModel::where('id',$info->pos_id)->first();
            return view('backend::shop.ad.create_and_edit',[
                'page_title'=>'编辑广告',
                'info'=>$info,
                'pos_info'=>$ad_pos
            ]);
        }
    }

    /**
     * @title 启用/禁用广告
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function switchAd($id)
    {
        $info = AdModel::find($id);
        if(!$info){
            return SelfResponse::error('查无数据');
        }

        $info->status = abs($info->status-1);
        if(!$info->save()){
            return SelfResponse::error('操作失败');
        }
        if($info->status<1){
            AdPosModel::where('id',$info->pos_id)->decrement('use_number',1);
        }else{
            AdPosModel::where('id',$info->pos_id)->increment('use_number',1);
        }
        return SelfResponse::success('操作成功');
    }

    /**
     * @title 删除广告
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function delete(Request $request)
    {
        $id = array_filter(str2arr($request->input('id')));
        if(!$id){
            return SelfResponse::error('请选择要删除的数据');
        }
        $pos_ids = AdModel::whereIn('id',$id)->pluck('pos_id');
        if(AdModel::whereIn('id',$id)->delete()){
            $count = array_count_values($pos_ids->toArray());
            foreach ($count as $pos_id=>$num){
                AdPosModel::where('id',$pos_id)->decrement('use_number',$num);
            }
            return SelfResponse::success('删除成功');
        }
        return SelfResponse::error('删除失败');
    }

    public function clearAdPos()
    {
        $ad_pos = AdPosModel::with('ad')->chunkById(100,function ($ads){
            foreach ($ads as $ad){
                $count = count($ad->ad);
                if($count!=$ad->use_number){
                    AdPosModel::where('id',$ad->id)->update(['use_number'=>$count]);
                }
            }
        });
        return SelfResponse::success('清理成功');
    }


}